import React from 'react';
import { observer, inject } from 'mobx-react';
import { WeaErrorPage,WeaTools, WeaLocaleProvider } from 'ecCom';
import {WeaTableNew} from 'comsMobx';
const WeaTable = WeaTableNew.WeaTable;
import * as _ from '../../util/common';
import '../../style/formResource.less';
const getLabel = WeaLocaleProvider.getLabel;

@observer
class Resource extends React.Component {
    constructor(props) {
		super(props);
    }
    componentDidMount(){
        const { wfFormResourceStore,commonParam } = this.props;
        let baseParam = {};
        const range = ["requestid", "isrequest", "desrequestid", "iswfshare", "authStr", "authSignatureStr", "f_weaver_belongto_userid", "f_weaver_belongto_usertype", "isurger","nodeid", "isurger","iscowork","coworkid","agentType","agentorByAgentId"];
        range.map(key =>{
            baseParam[key] = commonParam[key];
        });
        wfFormResourceStore.setBaseParam(baseParam);
        wfFormResourceStore.switchTab("0");
    }
    render(){
        const { wfFormResourceStore } = this.props;
        const { tabKey,tableStore } = wfFormResourceStore;
    	const menu = [
			{title:getLabel('332',"全部"), key:'0',},
			{title:getLabel('22105',"相关流程"), key:'1'},
			{title:getLabel('857',"相关文档"), key:'2'},
			{title:getLabel('22194',"相关附件"), key:'3'},
		];
        return (
            <div className='wf-resources' >
                <div className='wf-resources-tab'>
                    {
                        menu.map(t =>{
                            const isChecked = t.key == tabKey;
                            const _style = isChecked ? {background:'#4bb1fb', color:'#fff'} : {background:'transparent', color:'#848484'};
                            return <div className='wf-resources-tab-item' style={_style} onClick={this.switchTab.bind(this, t.key)}>
                                {t.title}
                            </div>
                        }
                    )}
                </div>
                <WeaTable ecId={`${this && this.props && this.props.ecId || ''}_WeaTable@yyvzcb`}
                    comsWeaTableStore={tableStore}
                    hasOrder={true}
                    needScroll={true} 
                />
            </div>
        )
    }
    switchTab(key){
        const { wfFormResourceStore } = this.props;
        wfFormResourceStore.switchTab(key);
    }
}

export default WeaTools.tryCatch(React, 
    props => <WeaErrorPage ecId={`${this && this.props && this.props.ecId || ''}_WeaErrorPage@pqmoff`} msg={ props.error ? props.error : getLabel(383324,'对不起，该页面异常，请联系管理员')} />,
    {error: ""}
)(Resource);

const resourceOperate = {
    downLoad: function(id, otherpara) {
        const parArr = otherpara.split("+");
        const currequestid = parArr[0];
        const authStr = parArr[1];
        const authSignatureStr = parArr[2];
        WfForm.downLoadFile(id);
        return;
    },
    openReq: function(id, otherpara) {
        const parArr = otherpara.split("+");
        const currequestid = parArr[0];
        const authStr = parArr[1];
        const authSignatureStr = parArr[2];
        window.open(`/workflow/request/ViewRequestForwardSPA.jsp?isrequest=1&requestid=${id}&authStr=${authStr}&authSignatureStr=${authSignatureStr}`);
        return;
    },
    openDoc: function(id, otherpara) {
        const parArr = otherpara.split("+");
        const currequestid = parArr[0];
        const authStr = parArr[1];
        const authSignatureStr = parArr[2];
        const globalStore = WfForm.getGlobalStore();
        const { docViewUrl } = globalStore.commonParam;
        window.open(`${docViewUrl}?isrequest=1&id=${id}&requestid=${currequestid}&authStr=${authStr}&authSignatureStr=${authSignatureStr}`);
        return;
    }
};
window.resourceOperate = resourceOperate;